import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_widgets/config/kq_global.dart';
import 'package:kq_flutter_widgets/resources/images.dart';
import 'package:kq_flutter_widgets/theme/kq_theme_manager.dart';
import 'package:kq_flutter_widgets/widgets/button/kq_icon_button.dart';
import 'package:kq_flutter_widgets/widgets/divider/kq_divider.dart';

import '../../../resources/kq_theme_colors.dart';

class KqBottomSheetView extends StatefulWidget {
  ///整体水平布局
  final CrossAxisAlignment crossAxisAlignment;

  ///中间自定义内容
  final Widget child;

  ///中间自定义固定高度,childHeight有值则为固定高度，无值则为自定义高度，但是最大为childMaxHeight
  final double? childHeight;

  ///中间自定义最大高度，默认为500
  final double? childMaxHeight;

  ///是否显示标题栏底部的线条，默认true
  final bool isTitleBottomLine;

  ///是否显示底部button组合，默认true
  final bool isBottomBtn;

  ///关闭按钮宽度
  final double? iconWidth;

  ///关闭按钮高度
  final double? iconHeight;

  ///关闭按钮距离右边的距离
  final double? iconRight;

  ///点击事件
  final Function(int index)? tabIndexBlock;

  ///标题
  final String title;

  ///按钮文案大小 默认16，
  final double? fontSize;

  ///标题样式，默认大小16，颜色262626
  final TextStyle? titleStyle;

  ///左边按钮文案，默认为取消
  final String? cancelStr;

  ///左按钮文案颜色#3A559B
  final Color? cancelColor;

  ///左边删除按钮自定义
  final Widget? cancelCustomWidget;

  ///左侧按钮背景色,默认为白色
  final Color? cancelBgColor;

  ///右边按钮文案，默认为确定
  final String? confirmStr;

  ///右边按钮文案颜色默认白色
  final Color? confirmColor;

  ///右侧按钮背景色，默认为#3A559B
  final Color? confirmBgColor;

  ///右边确认按钮自定义
  final Widget? confirmCustomWidget;

  const KqBottomSheetView({
    Key? key,
    required this.child,
    this.childMaxHeight,
    this.childHeight,
    this.iconRight,
    required this.title,
    this.crossAxisAlignment = CrossAxisAlignment.start,
    this.isBottomBtn = true,
    this.iconWidth,
    this.iconHeight,
    this.titleStyle,
    this.fontSize,
    this.tabIndexBlock,
    this.isTitleBottomLine = true,
    this.cancelStr,
    this.cancelColor,
    this.cancelBgColor,
    this.cancelCustomWidget,
    this.confirmStr,
    this.confirmColor,
    this.confirmBgColor,
    this.confirmCustomWidget,
  }) : super(key: key);

  @override
  State<KqBottomSheetView> createState() => _KqBottomSheetViewState();
}

class _KqBottomSheetViewState extends State<KqBottomSheetView> {
  @override
  void initState() {
    super.initState();
  }

  ///按钮样式
  Widget senderBtn(
      String title, Color bgColor, Color textColor, double fontSize) {
    return Container(
      color: bgColor,
      alignment: Alignment.center,
      child: Text(
        title,
        style: TextStyle(fontSize: fontSize, color: textColor),
      ),
    );
  }

  Widget _buildBottomBtn() {
    return SizedBox(
      height: 44.r,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(
              child: InkWell(
            child: widget.cancelCustomWidget ??
                senderBtn(
                    widget.cancelStr ?? '取消',
                    widget.cancelBgColor ?? KqThemeManager.getCommonConfig().itemBgColor ?? KqThemeColors.bgWhite,
                    widget.cancelColor ?? KqThemeManager.getCommonConfig().itemTitleColor ?? KqThemeColors.textBlue,
                    widget.fontSize ?? 16.sp),
            onTap: () {
              if (widget.tabIndexBlock != null) {
                widget.tabIndexBlock!(0);
              }
            },
          )),
          Expanded(
              child: InkWell(
            child: widget.confirmCustomWidget ??
                senderBtn(
                    widget.confirmStr ?? '确定',
                    widget.confirmBgColor ?? KqThemeManager.getCommonConfig().mainColor ?? KqThemeColors.textBlue,
                    widget.confirmColor ?? KqThemeColors.bgWhite,
                    widget.fontSize ?? 16.sp),
            onTap: () {
              if (widget.tabIndexBlock != null) {
                widget.tabIndexBlock!(1);
              }
            },
          ))
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: KqThemeManager.getDialogConfig().bottomSheetBgColor ?? KqThemeColors.bgWhite,
      height: widget.childHeight != null
          ? widget.childHeight! +
              48.r +
              (widget.isBottomBtn ? 44.r : 0) +
              KqScreenUtil().bottomBarHeight +
              (widget.isTitleBottomLine ? 1 : 0)
          : null,
      child: Column(
        crossAxisAlignment: widget.crossAxisAlignment,
        mainAxisSize:
            widget.childHeight != null ? MainAxisSize.max : MainAxisSize.min,
        children: [
          Stack(
            children: [
              Container(
                alignment: Alignment.center,
                width: double.infinity,
                height: 48.r,
                child: Text(
                  widget.title,
                  style: widget.titleStyle ??
                      TextStyle(fontSize: 16.sp, color: KqThemeManager.getDialogConfig().titleColor ?? KqThemeColors.text26),
                ),
              ),
              Positioned(
                right: widget.iconRight ?? 8.r,
                child: KqIconButton(
                  size: 48.r,
                  onPressed: () => Navigator.pop(context),
                  icon: Image.asset(
                    Images.commonIcGuanbi,
                    height: widget.iconHeight ?? 44.r,
                    width: widget.iconWidth ?? 44.r,
                    package: KqGlobal.packageName,
                    color: KqThemeManager.getDialogConfig().titleColor ?? KqThemeColors.text26,
                  ),
                ),
              )
            ],
          ),
          KqDivider(height: widget.isTitleBottomLine ? 1 : 0),
          Container(
              constraints: BoxConstraints(
                  maxHeight: widget.childHeight != null
                      ? widget.childHeight!
                      : widget.childMaxHeight ?? 500.r),
              child: widget.child),
          widget.isBottomBtn ? _buildBottomBtn() : Container(),
        ],
      ),
    );
  }
}
